package org.jeecg.modules.sanitation2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.student.entity.LwgyBaseStudent;
import org.jeecg.modules.sanitation2.entity.*;
import org.jeecg.modules.sanitation2.mapper.LwgyScoreSanitationMapper1;
import org.jeecg.modules.sanitation2.mapper.LwgyTargetTypeMapper;
import org.jeecg.modules.sanitation2.service.ILwgyScoreSanitationService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 卫生打分
 * @Author: jeecg-boot
 * @Date:   2021-04-29
 * @Version: V1.0
 */
@Service("testService")
public class LwgyScoreSanitationServiceImpl extends ServiceImpl<LwgyScoreSanitationMapper1, LwgyScoreSanitation> implements ILwgyScoreSanitationService {

	@Autowired
	private LwgyScoreSanitationMapper1 lwgyScoreSanitationMapper;
	@Autowired
	private LwgyTargetTypeMapper lwgyTargetTypeMapper;


	@Override
	public IPage<LwgyScoreSanitation> queryPageListCustomScore(Page<LwgyScoreSanitation> page,String scaleBatches,String studentCampus,String studentCollege) {
		return lwgyScoreSanitationMapper.queryPageListCustomScore(page,scaleBatches,studentCampus,studentCollege);
	}

	@Override
	public List<ImageZip> findBySql() {
		return lwgyScoreSanitationMapper.findBySql();
	}


	@Override
	public void executeSql(String sql) {
		lwgyScoreSanitationMapper.executeSql(sql);
	}


	@Override
	@Transactional
	public void saveMain(LwgyScoreSanitation lwgyScoreSanitation, List<LwgyTargetType> lwgyTargetTypeList) {
		lwgyScoreSanitationMapper.insert(lwgyScoreSanitation);
		if(lwgyTargetTypeList!=null && lwgyTargetTypeList.size()>0) {
			for(LwgyTargetType entity:lwgyTargetTypeList) {
				//外键设置
				entity.setSanitationId(lwgyScoreSanitation.getId());
				lwgyTargetTypeMapper.insert(entity);
			}
		}
	}


	@Override
	@Transactional
	public void updateMain(LwgyScoreSanitation lwgyScoreSanitation,List<LwgyTargetType> lwgyTargetTypeList) {
		lwgyScoreSanitationMapper.updateById(lwgyScoreSanitation);

		//1.先删除子表数据
		lwgyTargetTypeMapper.deleteByMainId(lwgyScoreSanitation.getId());

		//2.子表数据重新插入
		if(lwgyTargetTypeList!=null && lwgyTargetTypeList.size()>0) {
			for(LwgyTargetType entity:lwgyTargetTypeList) {
				//外键设置
				entity.setSanitationId(lwgyScoreSanitation.getId());
				lwgyTargetTypeMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		lwgyTargetTypeMapper.deleteByMainId(id);
		lwgyScoreSanitationMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			lwgyTargetTypeMapper.deleteByMainId(id.toString());
			lwgyScoreSanitationMapper.deleteById(id);
		}
	}

	@Override
	public IPage<LwgyScoreSanitation> statisticalQuery(Page<LwgyScoreSanitation> page, QueryWrapper<StatisticalQuery> queryWrapper) {
		return page.setRecords(lwgyScoreSanitationMapper.statisticalQuery(page,queryWrapper));
	}

    @Override
    public List<StatisticalDTO> getStatistical() {
		return lwgyScoreSanitationMapper.getStatistical();
    }

    @Override
    public List<String[]> queryHealthScoreInfo(String weeks) {

		List<HealthScoreInfo> list = lwgyScoreSanitationMapper.queryHealthScoreInfo(weeks);
		List<String[]> strings = new ArrayList<>(list.size());
		String[] string;
		for (HealthScoreInfo info : list) {
			string = new String[]{
					info.getBuildingName(),
					info.getRoomName(),
					info.getTotalScore(),
					info.getWeeks(),
					info.getRemark()

			};
			strings.add(string);
		}
        return strings;
    }

	@Override
	public void insertScoreInfo(List<LwgyScoreSanitation> roomList) {
		lwgyScoreSanitationMapper.insertScoreInfo(roomList);
	}

	@Override
	public List<LwgyScoreSanitation> scoreSearch(String scaleBatches) {
		return lwgyScoreSanitationMapper.scoreSearch(scaleBatches);
	}


}
